package model;

import java.io.Serializable;
import javax.persistence.*;

import java.util.List;

/**
 * The persistent class for the users database table.
 * 
 */
@Entity
@Table(name = "users")
@NamedQueries({
		@NamedQuery(name = "authenticate", query = "SELECT u FROM User u WHERE u.userName = :username AND u.password = :password"),
		@NamedQuery(name = "usersWhoBoughtSeats", query = "SELECT distinct s.user from Seat s where s.projection.id = :id and s.sold = false"),
		@NamedQuery(name = "usersWhoAttendedProjection", query = "SELECT distinct s.user from Seat s where s.projection.id = :id and s.sold = true and s.attended = true"),
		@NamedQuery(name = "getUserByUsername", query = "SELECT u from User u where u.userName = :username")})
public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@Column(unique = true, nullable = false)
	private int id;

	@Column(nullable = false, length = 64)
	private String password;

	@Column(nullable = false, length = 50)
	private String userName;

	// bi-directional many-to-one association to Seat
	@OneToMany(mappedBy = "user")
	private List<Seat> seats;

	@Column(name = "role", nullable = false, length = 30)
	private String role;

	public User() {
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserName() {
		return this.userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public List<Seat> getSeats() {
		return this.seats;
	}

	public void setSeats(List<Seat> seats) {
		this.seats = seats;
	}

	// TODO tova ne tryabva da e tuka
	public Seat addSeat(Seat seat) {
		getSeats().add(seat);
		seat.setUser(this);

		return seat;
	}

	// TODO tova ne tryabva da e tuka
	public Seat removeSeat(Seat seat) {
		getSeats().remove(seat);
		seat.setUser(null);

		return seat;
	}

	public String getRole() {
		return this.role;
	}

	public void setRole(String role) {
		this.role = role;
	}

}